#include "js-engine.h"
#include <glib/gi18n.h>
#include "misc.h"

static js_engine *engine = NULL;
static JSBool plugin_removeMe(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);
static JSBool plugin_showDetailsView(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);
static JSBool plugin_closeDetailsView(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);
static JSBool plugin_showOptionsDialog(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);

static JSClass pluginClass = {
    "plugin", 0,
    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, 
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
    JSCLASS_NO_OPTIONAL_MEMBERS
};

static JSFunctionSpec plugin_methods[] = {
    {"removeMe", plugin_removeMe, 1, 0, 0},
    {"showDetailsView", plugin_showDetailsView, 4, 0, 0},
    {"closeDetailsView", plugin_closeDetailsView, 0, 0, 0},
    {"showOptionsDialog", plugin_showOptionsDialog, 0, 0, 0},
    {NULL, NULL, 0, 0, 0}
};

static JSBool plugin_removeMe(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    return JS_TRUE;
}

static JSBool plugin_showDetailsView(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    return JS_TRUE;
}

static JSBool plugin_closeDetailsView(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    return JS_TRUE;
}

static JSBool plugin_showOptionsDialog(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    option_dialog_show(engine);
    return JS_TRUE;
}

JSBool pluginClass_init(js_engine *new)
{
    jsval plugin_jsval;
    JSObject *plugin;
    engine = new;

    plugin = JS_DefineObject(engine->cx, engine->glob, "plugin", &pluginClass, NULL, JSPROP_PERMANENT);
    g_return_val_if_fail(plugin, JS_FALSE);
    g_return_val_if_fail(JS_DefineFunctions(engine->cx, plugin, plugin_methods), JS_FALSE);
    plugin_jsval = OBJECT_TO_JSVAL(plugin);
    g_return_val_if_fail(JS_SetProperty(engine->cx, engine->glob, "pluginHelper", &plugin_jsval), JS_FALSE);
    return JS_TRUE;
}
